﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Core;
using YunSpace.Domain.Entity.WeChat;
using YunSpace.Domain.IRepository.WeChat;
using YunSpace.Repository.WeChat;
using System.Collections.Generic;
using System.Linq;
using YunSpace.Data;
using System.Data.Common;
using MySql.Data.MySqlClient;
#endregion

namespace YunSpace.Application.WeChat
{
    public class SettingItemsApp
    {
        private ISettingItemsRepository service = new SettingItemsRepository();

        public List<SettingItemsEntity> GetList(Pagination pagination, string queryJson)
        {
            var expression = ExtLinq.True<SettingItemsEntity>();
            var queryParam = queryJson.ToJObject();
            if (!queryParam["keyword"].IsEmpty())
            {
                string keyword = queryParam["keyword"].ToString();
                expression = expression.And(t => t.F_Title.Contains(keyword));
            }
            if (!queryParam["type"].IsEmpty())
            {
                string type = queryParam["type"].ToString();
                expression = expression.And(t => t.F_SettingId == type);
            }
            var result = service.FindList(expression, pagination);

            return result;
        }

        public SettingItemsEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void SubmitForm(SettingItemsEntity settingitemsEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                settingitemsEntity.Modify(keyValue);
                service.Update(settingitemsEntity);
            }
            else
            {
                settingitemsEntity.Create();
                // settingitemsEntity.F_Category = 2;
                service.Insert(settingitemsEntity);
            }
        }



        /// <summary>
        /// 获取小程序首页数据
        /// </summary>
        /// <returns></returns>
        public List<SettingItemsEntity> GetItemsByCode(string F_Code)
        {
            using (var db = new RepositoryBase())
            {
                var strSql = @"select wsi.* from WeChat_SettingItems wsi inner join WeChat_Setting ws on ws.F_Id=wsi.F_SettingId and ws.F_Code=@F_Code where wsi.F_State=1 order by wsi.F_Sort";
                DbParameter[] parameter =
                {
                    new MySqlParameter("@F_Code", F_Code)
                };
                return db.FindList<SettingItemsEntity>(strSql, parameter);
            }
        }
    }
}

